/* eslint-disable */
export const KsCon = {
    encTable :[  /*Base64编码表*/
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
        'I', 'J', 'K', 'L', 'M', 'N', 'O' ,'P',
        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
        'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z', '0', '1', '2', '3',
        '4', '5', '6', '7', '8', '9', '+', '/'
    ],
    // decTable:[ /*Base64解码表*/
    //     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    //     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    //     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    //     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    //     -1, -1, -1, 62, -1, -1, -1, 63, 52, 53,
    //     54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
    //     -1, -1, -1, -1, -1, 00, 01, 02, 03, 04,
    //     05, 06, 07, 08, 09, 10, 11, 12, 13, 14,
    //     15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
    //     25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
    //     29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
    //     39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
    //     49, 50, 51, -1, -1, -1, -1, -1
    // ],
    encUTF8: function(str) { /*将任意字符串按UTF8编码*/
        var code, res =[], len =str.length;
        var byte1, byte2, byte3, byte4, byte5, byte6;
        for (var i = 0; i < len; i++) {
            //Unicode码：按范围确定字节数
            code = str.charCodeAt(i);

            //单字节ascii字符：U+00000000 – U+0000007F	0xxxxxxx
            if (code > 0x0000 && code <= 0x007F) res.push(code);

            //双字节字符：U+00000080 – U+000007FF	110xxxxx 10xxxxxx
            else if (code >= 0x0080 && code <= 0x07FF) {
                byte1 = 0xC0 | ((code >> 6) & 0x1F);
                byte2 = 0x80 | (code & 0x3F);
                res.push(byte1, byte2);
            }

            //三字节字符：U+00000800 – U+0000FFFF	1110xxxx 10xxxxxx 10xxxxxx
            else if (code >= 0x0800 && code <= 0xFFFF) {
                byte1 = 0xE0 | ((code >> 12) & 0x0F);
                byte2 = 0x80 | ((code >> 6) & 0x3F);
                byte3 = 0x80 | (code & 0x3F);
                res.push(byte1, byte2, byte3);
            }

            //四字节字符：U+00010000 – U+001FFFFF	11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
            else if (code >= 0x00010000 && code <= 0x001FFFFF) {
                byte1 =0xF0 | ((code>>18) & 0x07);
                byte2 =0x80 | ((code>>12) & 0x3F);
                byte3 =0x80 | ((code>>6) & 0x3F);
                byte4 =0x80 | (code & 0x3F);
                res.push(byte1, byte2, byte3, byte4);
            }

            //五字节字符：U+00200000 – U+03FFFFFF	111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            else if (code >= 0x00200000 && code <= 0x03FFFFFF) {
                byte1 =0xF0 | ((code>>24) & 0x03);
                byte2 =0xF0 | ((code>>18) & 0x3F);
                byte3 =0x80 | ((code>>12) & 0x3F);
                byte4 =0x80 | ((code>>6) & 0x3F);
                byte5 =0x80 | (code & 0x3F);
                res.push(byte1, byte2, byte3, byte4, byte5);
            }

            //六字节字符：U+04000000 – U+7FFFFFFF	1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            else if (code >= 0x04000000 && code <= 0x7FFFFFFF) {
                byte1 =0xF0 | ((code>>30) & 0x01);
                byte2 =0xF0 | ((code>>24) & 0x3F);
                byte3 =0xF0 | ((code>>18) & 0x3F);
                byte4 =0x80 | ((code>>12) & 0x3F);
                byte5 =0x80 | ((code>>6) & 0x3F);
                byte6 =0x80 | (code & 0x3F);
                res.push(byte1, byte2, byte3, byte4, byte5, byte6);
            }
        }
        return res;
    },
    encode: function(str) {
        /**
         * 将任意字符串用Base64加密
         * str：要加密的字符串
         * utf8编码格式
         */
        if (!str) return '';
        var bytes = this.encUTF8(str);
        var i = 0, len = bytes.length, res = [];
        var c1, c2, c3;
        while (i < len) {
            c1 = bytes[i++] & 0xFF;
            res.push(this.encTable[c1 >> 2]);
            //结尾剩一个字节补2个=
            if (i == len) {
                res.push(this.encTable[(c1 & 0x03) << 4], '==');
                break;
            }

            c2 = bytes[i++];
            //结尾剩两个字节补1个=
            if (i == len) {
                res.push(this.encTable[((c1 & 0x03) << 4) | ((c2 >> 4) & 0x0F)]);
                res.push(this.encTable[(c2 & 0x0F) << 2], '=');
                break;
            }

            c3 = bytes[i++];
            res.push(this.encTable[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
            res.push(this.encTable[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
            res.push(this.encTable[c3 & 0x3F]);
        }
        return res.join('');
    },
    // 生成全球唯一标示
    generateUid: function () {
        var d = new Date().getTime();
        if (window.performance && typeof window.performance.now === "function") {
            d += performance.now(); //use high-precision timer if available
        }
            var uid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uid;
    }
}
